package com.funtl.framework.chart.ofc;

//Author: abrahu
public class OFC2Builder {
	private StringBuilder sb;

	public String createChart(OFC2Chart chart) {
		sb = new StringBuilder();
		// create {
		sb.append("{");
		// create title
		OFC2Title title = chart.getTitle();
		if (title != null) {
			setTextandStyle("title", title.getText(), title.getStyle(),
					TITLE_TEST, TITLE_STYLE);
		}
		// create x_legend
		OFC2Legend lx = chart.getX_legend();
		if (lx != null) {
			setTextandStyle("x_legend", lx.getText(), lx.getStyle(),
					LEGEND_TEXT, LEGEND_STYLE);
		}
		// create y_legend
		OFC2Legend ly = chart.getY_legend();
		if (ly != null) {
			setTextandStyle("y_legend", ly.getText(), ly.getStyle(),
					LEGEND_TEXT, LEGEND_STYLE);
		}
		// create elements
		sb.append("\"elements\":[");
		for (OFC2Element e : chart.getElements()) {
			sb.append("{");
			if (e.getAttributes() != null) {
				sb.append(e.getAttributes());
			} else {
				sb.append(ELEMENT_ATTR);
			}
			if (e.getText() != null) {
				sb.append(",\"text\":\"" + e.getText() + "\"");
			} else {
				System.out.println("OFC2's element's text is empty!");
			}
			if (e.getValues() != null) {
				sb.append(",\"values\":[" + e.getValues() + "]");
			} else {
				System.out.println("OFC2's element's value is empty!");
			}
			sb.append("},");
		}
		if (sb.charAt(sb.length() - 1) == ',')
			sb.deleteCharAt(sb.length() - 1);
		sb.append("],");
		// create x_axis
		OFC2Axis xa = chart.getX_axis();
		if (xa == null) {
			xa = new OFC2Axis();
		}
		sb.append("\"x_axis\":{");
		if (xa.getAttributes() != null) {
			sb.append(xa.getAttributes());
		} else {
			sb.append(XAXIS_ATTR);
		}
		if (xa.getLables() != null) {
			sb.append(",\"labels\":{\"labels\":[" + xa.getLables() + "]}");
		}
		sb.append("},");
		// create y_axis
		OFC2Axis ya = chart.getY_axis();
		if (ya != null) {
			sb.append("\"y_axis\":{");
			if (ya.getAttributes() != null) {
				sb.append(ya.getAttributes());
			} else {
				sb.append(YAXIS_ATTR);
			}
			sb.append("}");
		}

		// create }
		if (sb.charAt(sb.length() - 1) == ',')
			sb.deleteCharAt(sb.length() - 1);
		sb.append("}");

		return sb.toString();
	}

	public final String TITLE_TEST = "Title";
	public final String TITLE_STYLE = "{font-size: 20px; color:#0000ff; font-family: Verdana; text-align: center;}";
	public final String LEGEND_TEXT = "Legend";
	public final String LEGEND_STYLE = "{color: #736AFF; font-size: 12px;}";
	public final String ELEMENT_ATTR = "\"type\":\"line\", \"alpha\":0.5, \"colour\":\"#9933CC\", \"font-size\": 10";
	public final String XAXIS_ATTR = "\"stroke\":1, \"tick_height\":10, \"colour\":\"#d000d0\", \"grid_colour\":\"#00ff00\"";
	public final String YAXIS_ATTR = "\"stroke\":4, \"tick_length\": 3, \"colour\":\"#d000d0\", \"grid_colour\": \"#00ff00\", \"offset\":0, \"max\":20";

	private void setTextandStyle(String unitName, String text, String style,
			String defaulTest, String defaultStyle) {
		sb.append("\"" + unitName + "\":{");
		if (text != null) {
			sb.append("\"text\":\"" + text + "\",");
		} else {
			sb.append("\"text\":\"" + defaulTest + "\",");
		}
		if (style != null) {
			sb.append("\"style\":\"" + style + "\"");
		} else {
			sb.append("\"style\":\"" + defaultStyle + "\"");
		}
		sb.append("},");
	}
}
